package com.wn.cloud.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wn.cloud.api.PaymentHystrixClient;
import com.wn.cloud.common.constant.ResponseEnum;
import com.wn.cloud.common.response.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @FileName: OrderController
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/3/8 - 14:59
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "paymentFallbackMethod")
public class OrderController {

    @Autowired
    private PaymentHystrixClient paymentHystrixClient;

    @GetMapping("/{id}")
    @HystrixCommand
    DataResult<String> getOne(@PathVariable("id") long id) {
        return paymentHystrixClient.getOne(id);
    }

    @GetMapping("/timeout")
    @HystrixCommand(fallbackMethod = "getTimeoutFallback", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    })
    DataResult<String> getTimeout() {
        return paymentHystrixClient.getTimeout();
    }

    DataResult<String> getTimeoutFallback() {
        return DataResult.genDataResult(ResponseEnum.UNKNOWN_EXCEPTION, "消费端降级处理");
    }

    DataResult<String> paymentFallbackMethod() {
        return DataResult.genDataResult(ResponseEnum.FALLBACK, "消费端全局fallback");
    }
}
